<!--
 * @Descripttion: (客户端使用处表/MES_CLIENT_USAGES)
 * @version: (1.0)
 * @Author: (liutao)
 * @Date: (2022-07-28)
 * @LastEditors: (liutao)
 * @LastEditTime: (2022-07-28)
-->
<template>
  <div>
    <!-- :model属性用于表单验证使用 比如下面的el-form-item 的 prop属性用于对表单值进行验证操作 -->
    <!-- <el-form :model="queryParams" size="small" label-position="right" inline ref="queryForm" :label-width="labelWidth" v-show="showSearch" 
      @submit.native.prevent>
          				                
      <el-form-item label="客户端ID" prop="clientId">
        <el-input v-model.number="queryParams.clientId" placeholder="请输入客户端ID" :disabled="true"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          v-hasPermi="['business:mes_client_usages:add']"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 数据区域 -->
    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      highlight-current-row
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <!-- <el-table-column prop="usageId" label="使用处ID" align="center" /> -->
      <!-- <el-table-column prop="clientId" label="客户端ID" align="center" :hidden="true" /> -->
      <el-table-column prop="workShopCode" label="产线" align="center" :formatter="workShopCodeFormat">
      </el-table-column>
      <el-table-column prop="workShopId" label="工位" align="center" :formatter="stationCodeFormat">
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center" :formatter="statusFormat"/>
       
      <el-table-column
        prop="ipAddress"
        label="IP地址"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column prop="uaUrl" label="UA地址" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-hasPermi="['business:mes_client_usages:edit']"
            type="success"
            icon="el-icon-edit"
            title="编辑"
            @click="handleUpdate(scope.row)"
          ></el-button>
          <el-button
            size="mini"
            v-hasPermi="['business:mes_client_usages:delete']"
            type="danger"
            icon="el-icon-delete"
            title="删除"
            @click="handleDelete(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      class="mt10"
      background
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户端使用处表对话框 -->
    <el-dialog
      :title="title"
      :lock-scroll="false"
      :visible.sync="open"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        :label-width="formLabelWidth"
      >
        <el-row :gutter="20">
          <el-col :lg="12" v-if="opertype == 2">
            <el-form-item label="使用处ID">{{ form.usageId }}</el-form-item>
          </el-col>

          <el-col :lg="12" v-show="true">
            <el-form-item label="客户端ID" prop="clientId">
              <el-input
                v-model="form.clientId"
                placeholder="请输入客户端ID"
                :disabled="true"
              />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工位" prop="workShopId">
              <el-select
                v-model="form.workShopId"
                placeholder="请选择工位编码"
                filterable
              >
                <el-option
                  v-for="item in workshipIdOptions"
                  :key="item.shopId"
                  :label="item.shopName"
                  :value="item.shopId"
                >
                  <span style="float: left">{{ item.shopCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{
                    item.shopName
                  }}</span></el-option
                >
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="item in statusOptions"
                  :key="item.dictValue"
                  :label="item.dictValue"
                  >{{ item.dictLabel }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="IP地址" prop="ipAddress">
              <el-input v-model="form.ipAddress" placeholder="请输入IP地址" />
            </el-form-item>
          </el-col>

          <el-col :lg="24">
            <el-form-item label="UA地址" prop="uaUrl">
              <el-input v-model="form.uaUrl" placeholder="请输入UA地址" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listMes_client_usages,
  getMes_client_usages,
  delMes_client_usages,
  addMes_client_usages,
  updateMes_client_usages,
  exportMes_client_usages,
} from "@/api/client/mes_client_usages";

import { listMes_client_pages_relation } from "@/api/client/mes_client_pages_relation.js";
import { getShopByType,getShop } from "@/api/base/shop";
// import { getShop } from "@/api/base/shopConfig";
export default {
  name: "clientData",
  props: {
    clientId: {
      type: Number,
      default: 0,
    },
  },
  watch: {
    clientId: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.queryParams.clientId = newVal;
          this.defaultclientId = newVal;
          this.getList();
        }
      },
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      // 默认终端ID
      defaultclientId: 0,
      labelWidth: "100px",
      formLabelWidth: "100px",
      // 选中usageId数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: undefined,
        sortType: undefined,
        clientId: undefined,
      },
      params: {
        clientId: undefined,
      },
      // 弹出层标题
      title: "",
      // 操作类型 1、add 2、edit
      opertype: 0,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      columns: [
        { index: 0, key: "usageId", label: `使用处ID`, checked: true },
        { index: 1, key: "clientId", label: `客户端ID`, checked: true },
        { index: 2, key: "workShopId", label: `工位ID`, checked: true },
        { index: 3, key: "status", label: `Y/N`, checked: true },
        { index: 4, key: "ipAddress", label: `IP地址`, checked: true },
        { index: 5, key: "uaUrl", label: `UA地址`, checked: true },
        { index: 6, key: "createTime", label: `创建时间`, checked: true },
        { index: 7, key: "delFlag", label: `删除标志`, checked: true },
        { index: 8, key: "updateBy", label: `更新人员`, checked: true },
        { index: 9, key: "updateTime", label: `更新时间`, checked: false },
        { index: 10, key: "createBy", label: `创建人员`, checked: false },
        { index: 11, key: "fIELD1", label: ``, checked: false },
        { index: 12, key: "fIELD2", label: ``, checked: false },
        { index: 13, key: "fIELD3", label: ``, checked: false },
        { index: 14, key: "fIELD4", label: ``, checked: false },
      ],
      // 工位ID选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      workshipIdOptions: [],
      // Y/N选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      statusOptions: [],
      // 数据列表
      dataList: [],
      pageList: [],
      lineOptions: [],
      // 总记录数
      total: 0,
      // 提交按钮是否显示
      btnSubmitVisible: true,
      // 表单校验
      rules: {
        // clientId: [
        //   { required: true, message: "终端ID不能为空", trigger: "blur" }
        // ],
        workShopId: [
          { required: true, message: "工位不能为空", trigger: "change" },
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" },
        ],
        ipAddress: [
          { required: true, message: "IP地址不能为空", trigger: "blur" },
        ],
        uaUrl: [{ required: true, message: "Ua地址不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    // 列表数据查询
    var shoptype = "4";
    getShopByType(shoptype).then((res) => {
      this.workshipIdOptions = res.data;
    });
    // 列表数据查询---翻译使用
    var shoptype1 = "3";
    getShopByType(shoptype1).then((res) => {
      this.lineOptions = res.data;
    });
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    // 查询数据
    getList() {
      this.loading = true;
      listMes_client_usages(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.dataList = res.rows;
          this.total = res.total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 重置数据表单
    reset() {
      this.form = {
        clientId: this.defaultclientId,
        workShopId: undefined,
        status: undefined,
        ipAddress: undefined,
        uaUrl: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        createBy: undefined,
      };
      this.resetForm("form");
    },
    // 重置查询操作
    resetQuery() {
      this.timeRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.usageId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 自定义排序
    sortChange(column) {
      if (column.prop == null || column.order == null) {
        this.queryParams.sort = undefined;
        this.queryParams.sortType = undefined;
      } else {
        this.queryParams.sort = column.prop;
        this.queryParams.sortType = column.order;
      }

      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.params.clientId = this.defaultclientId;
      listMes_client_pages_relation(this.params).then((res) => {
        if (res.code == 200) {
          this.pageList = res.rows;
          if (this.pageList.length > 0) {
            this.reset();
            this.open = true;
            this.title = "添加";
            this.opertype = 1;
          } else {
            this.msgError("请进行页面分配");
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.usageId || this.ids;

      this.$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
        .then(function () {
          return delMes_client_usages(Ids);
        })
        .then(() => {
          this.handleQuery();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.usageId || this.ids;
      getMes_client_usages(id).then((res) => {
        const { code, data } = res;
        if (code == 200) {
          this.open = true;
          this.title = "修改数据";
          this.opertype = 1;

          this.form = {
            ...data,
          };
        }
      });
    },
    //图片上传成功方法
    handleUploadSuccess(column, filelist) {
      this.form[column] = filelist;
    },
    // 车间翻译
     workShopCodeFormat(row, column) {
      var actions1 = [];
       Object.keys(this.workshipIdOptions).some((key) => {
        if (this.workshipIdOptions[key].shopId == "" + row.workShopId) {
          actions1.push(this.workshipIdOptions[key].parentId);
          return true;
        }
      });
      var actions = [];
      //产线翻译
      Object.keys(this.lineOptions).some((key) => {
        if (this.lineOptions[key].shopId == "" + actions1[0]) {
          actions.push(this.lineOptions[key].shopName);
          return true;
        }
      });
      return actions.join(" ");
    },
    // 工位编码字典翻译
    stationCodeFormat(row, column) {
      var actions = [];
      Object.keys(this.workshipIdOptions).some((key) => {
        if (this.workshipIdOptions[key].shopId == "" + row.workShopId) {
          actions.push(this.workshipIdOptions[key].shopName);
          return true;
        }
      });
      return actions.join(" ");
    },
    // Y/N字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(JSON.stringify(this.form));

          if (this.form.usageId != undefined && this.opertype === 1) {
            updateMes_client_usages(this.form)
              .then((res) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((err) => {
                //TODO 错误逻辑
                this.msgError("修改失败，联系管理员处理！");
              });
          } else {
            addMes_client_usages(this.form)
              .then((res) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((err) => {
                //TODO 错误逻辑
                this.msgError("新增失败，联系管理员处理！");
              });
          }
        }
      });
    },
  },
};
</script>